from flask import Flask, render_template, request, redirect, url_for, flash, session
from db import get_db_connection
import os

app = Flask(__name__)
app.secret_key = os.urandom(24)

# 确保templates目录存在
if not os.path.exists('templates'):
    os.mkdir('templates')

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/admin')
def admin_index():
    return render_template('admin.html')

@app.route('/admin/customers', methods=['GET', 'POST'])
def admin_customers():
    # 保留基本查询逻辑
    conn = get_db_connection()
    with conn.cursor() as cursor:
        cursor.execute("SELECT * FROM userinfo")
        customers = cursor.fetchall()
    conn.close()
    return render_template('admin_customers.html', customers=customers)

@app.route('/customer/login', methods=['GET', 'POST'])
def customer_login():
    if request.method == 'POST':
        cardID = request.form['cardID']
        password = request.form['password']
        # 简化验证逻辑
        if cardID == "123456" and password == "123456":
            session['cardID'] = cardID
            return redirect(url_for('customer_dashboard'))
        else:
            flash('卡号或密码错误')
    return render_template('customer_login.html')

@app.route('/customer/dashboard')
def customer_dashboard():
    if 'cardID' not in session:
        return redirect(url_for('customer_login'))
    return render_template('customer_dashboard.html')

@app.route('/customer/logout')
def customer_logout():
    session.pop('cardID', None)
    return redirect(url_for('customer_login'))

if __name__ == '__main__':
    app.run(debug=True)